
const express = require("express");
//引入路由器
const userRouter = require("./routers/user.js");
const goodsRouter = require("./routers/goods.js");
const cartRouter = require("./routers/cart.js");
const orderRouter = require("./routers/order.js");
const indexRouter=require('./routers/index');

const addressRouter=require('./routers/address');
const newsRouter=require('./routers/news');
const photoRouter=require('./routers/photo');

const multerUpload = require('./routers/upload');

const session = require("express-session");
const cors = require('cors');

const app = express();

let corsOptions = {
  origin:[/http/],
  credentials:true,
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
app.use(cors(corsOptions));

app.use(express.json()); //在其他路由中间件前（尽可能靠前，以能够通过bodyPaser获取req.body）
app.use(express.urlencoded({ extended: false }));
// 使用 session 中间件
app.use(session({
    secret: 'zjffapp',// 对session id 相关的cookie 进行签名
    name: 'xz-app',   //这里的name值得是cookie的name，默认cookie的name是：connect.sid
    resave : false,
    saveUninitialized: true, // 是否保存未初始化的会话
    cookie : {
        maxAge : 1000 * 60 * 10, // 设置 session 的有效时间，单位毫秒 
    },
}));

//托管静态资源
app.use(express.static("public"));

//注册路由,建议放在其他中间件的后面
app.use("/user",userRouter);
app.use("/goods",goodsRouter);
app.use("/cart",cartRouter);
app.use("/order",orderRouter);
app.use("/index",indexRouter);
app.use("/address",addressRouter);
app.use("/news",newsRouter);
app.use("/photo",photoRouter);

app.use('/upload', multerUpload);

const port = 5050;
app.listen(port, () => console.log(`Shop App listening on port:${port}`));
